﻿using System;
using System.Collections.Generic;
using System.Runtime.Remoting.Messaging;
using MySql.Data.MySqlClient;
using OrionApplication.OrionLib.Orion;
using System.Data;

namespace OrionApplication.OrionLib.Dao
{
    public class DaoMouvementTete : DaoBase
    {

        public DaoMouvementTete()
            : base()
        {
            this.TableName = "mouvementtete";
            this.ViewName = "vmouvementtete";
        }

        public MouvementTete SelectById(Int32 OrionId)
        {
            lParam.Clear();
            MouvementTete retour = new MouvementTete();
            String req = this.DebutSelect
            + this.TableName
            + " WHERE OrionId=@orionid";

            lParam.Add(new MySqlParameter("@OrionId", OrionId));

            MySqlDataReader dr = this.ExecuteQueryReader(req, lParam);

            if (dr.Read())
                retour = ChargeFromReader(dr);
            dr.Close();
            return retour;
        }

        public List<MouvementTete> SelectAllActif()
        {
            lParam.Clear();
            List<MouvementTete> retour = new List<MouvementTete>();
            String req = this.DebutSelect
            + this.TableName
            + " WHERE OrionEtat=1";

            MySqlDataReader dr = this.ExecuteQueryReader(req, lParam);

            while (dr.Read())
                retour.Add(ChargeFromReader(dr));
            dr.Close();

            return retour;
        }

        public DataSet SelectAllClientDSView(Int32 ClientId)
        {
            lParam.Clear();
            DataSet retour = new DataSet();
            if (this.TableName != string.Empty)
            {
                String req = "SELECT * FROM "
                    + this.ViewName
                    + " WHERE CodeClient=@CodeClient";

                lParam.Add(new MySqlParameter("@CodeClient", ClientId));

                retour = this.ExecuteQueryDataSet(req, lParam);
            }
            return retour;
        }

        public List<MouvementTete> SelectAll()
        {
            lParam.Clear();
            List<MouvementTete> retour = new List<MouvementTete>();
            String req = this.DebutSelect
            + this.TableName;

            MySqlDataReader dr = this.ExecuteQueryReader(req, lParam);

            while (dr.Read())
                retour.Add(ChargeFromReader(dr));
            dr.Close();

            return retour;
        }

        private MouvementTete ChargeFromReader(MySqlDataReader dr)
        {
            int i = 0;
            MouvementTete retour = new MouvementTete();
            retour.OrionId = dr.GetInt32("OrionId");

            i++;
            if (dr.IsDBNull(i))
                retour.OrionMouvementId = 0;
            else
                retour.OrionMouvementId = dr.GetInt32("OrionMouvementId");
            i++;
            if (dr.IsDBNull(i))
                retour.Commentaire = String.Empty;
            else
                retour.Commentaire = dr.GetString("Commentaire");
            i++;
            if (dr.IsDBNull(i))
                retour.Date = null;
            else
                retour.Date = dr.GetDateTime("Date");
            i++;
            if (dr.IsDBNull(i))
                retour.OrionClientId = 0;
            else
                retour.OrionClientId = dr.GetInt32("OrionClientId");

            retour.OrionCrea = dr.GetDateTime("OrionCrea");
            retour.OrionMaj = dr.GetDateTime("OrionMaj");
            retour.OrionUserId = dr.GetInt32("OrionUserId");
            retour.OrionEtat = dr.GetBoolean("OrionEtat");

            return retour;
        }

        public MouvementTete Update(MouvementTete oe)
        {
            lParam.Clear();
            oe.OrionMaj = DateTime.Now;

            MouvementTete retour = new MouvementTete();
            String req = "UPDATE " + TableName + " SET OrionMouvementId=@OrionMouvementId,Commentaire=@Commentaire,Date=@Date,OrionClientId=@OrionClientId,OrionUserId=@OrionUserId, OrionMaj=@OrionMaj WHERE OrionId=@OrionId";

            lParam.Add(new MySqlParameter("@OrionMouvementId", oe.OrionMouvementId));
            lParam.Add(new MySqlParameter("@Commentaire", oe.Commentaire));
            lParam.Add(new MySqlParameter("@Date", oe.Date));
            lParam.Add(new MySqlParameter("@OrionClientId", oe.OrionClientId));

            lParam.Add(new MySqlParameter("@OrionUserId", ((OrionUser)CallContext.GetData(OrionConfBase.OrionUserConnected)).OrionId));
            lParam.Add(new MySqlParameter("@OrionMaj", oe.OrionMaj));
            lParam.Add(new MySqlParameter("@OrionId", oe.OrionId));

            this.ExecuteNonQuery(req, lParam);
            retour = this.SelectById(oe.OrionId);

            return retour;
        }

        public MouvementTete Insert(MouvementTete oe)
        {
            lParam.Clear();
            oe.OrionCrea = DateTime.Now;
            oe.OrionMaj = DateTime.Now;

            MouvementTete retour = new MouvementTete();
            String req = "INSERT INTO " + TableName + "(OrionId,OrionMouvementId,Commentaire,Date,OrionClientId,OrionEtat, OrionUserId, OrionCrea, OrionMaj)  VALUES(NULL,@OrionMouvementId,@Commentaire,@Date,@OrionClientId,@OrionEtat,@OrionUserId,@OrionCrea,@OrionMaj)";
            lParam.Add(new MySqlParameter("@OrionMouvementId", oe.OrionMouvementId));
            lParam.Add(new MySqlParameter("@Commentaire", oe.Commentaire));
            lParam.Add(new MySqlParameter("@Date", oe.Date));
            lParam.Add(new MySqlParameter("@OrionClientId", oe.OrionClientId));
            lParam.Add(new MySqlParameter("@OrionEtat", oe.OrionEtat));
            lParam.Add(new MySqlParameter("@OrionUserId", ((OrionUser)CallContext.GetData(OrionConfBase.OrionUserConnected)).OrionId));
            lParam.Add(new MySqlParameter("@OrionCrea", oe.OrionCrea));
            lParam.Add(new MySqlParameter("@OrionMaj", oe.OrionMaj));

            this.ExecuteNonQuery(req, lParam);
            retour = this.SelectById(Convert.ToInt32(this.LastInsertId));

            return retour;
        }
    }
}